package com.glory.brepda.activity.mfg;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.GroupEqpListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.EquCallOutStateEntity;
import com.glory.brepda.entity.EquRoleListEntity;
import com.glory.brepda.entity.EquThirdStateEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;
import android.content.Intent;

import org.apache.commons.lang3.StringUtils;

public class EquipmentTransferCompleteActivity extends BaseActivity {
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.roleList)
    FrameLayout roleList;
    @BindView(R.id.arrow)
    ImageView arrow;
    @BindView(R.id.roleSelectLine)
    TextView roleSelectLine;

    @BindView(R.id.remark)
    FreeUI_ClearEditText remark;


    private FreeUI_GeneralPop parentStatePop;

    private Set<String> roleMap = new HashSet<>();

    private Map<String, EquRoleListEntity.QCRoleEntity> stateMap = new HashMap<>();

    private boolean isEmpty = false;

    private Activity activity;


    @Override
    protected int getLayoutResId() {
        return R.layout.equ_transfer_complete;
    }


    Bundle bundle;

    @Override
    protected void initView() {
        bundle = getIntent().getExtras();
        setMainLayout(llMain);
        activity = this;
        roleSelectLine.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });

    }

    @Override
    protected void initData() {
        getRoleList();

    }

    private void getRoleList() {
        FreeUI_EntityCallBack<BaseResponseBean<EquRoleListEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<EquRoleListEntity>>
                (new TypeToken<BaseResponseBean<EquRoleListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EquRoleListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    List<EquRoleListEntity.QCRoleEntity> tempStateList = response.body().getResponse().getBody().getROLELIST();
                    for (EquRoleListEntity.QCRoleEntity role : tempStateList) {
                        roleMap.add(role.getText());
                        stateMap.put(role.getText(), role);
                    }

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EquRoleListEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "getRoleList");
            }
        };
        //查询qc任务角色列表
        roleMap.clear();
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QUERYROLELIST");
        NetUtils.requestPostNet("IF.BREEQUCALLOUTMAINTAIN","Common", EquipmentTransferCompleteActivity.this,map,callBack);
    }

    @OnClick({R.id.roleList,R.id.submitButton})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.roleList) {

            parentStatePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), roleList.getWidth(), new ArrayList<>(roleMap), arrow, (codeS, i) -> {
                roleSelectLine.setText(codeS);
            });
            FreeApi_PopUtils.setArrowShow(arrow);
            parentStatePop.showAsDropDown(roleList, 0, 0);
        }else if (view.getId() == R.id.submitButton){
            if(remark.getText()!=null){
                if("".equals(remark.getText().toString())){
                    showToast("备注不能为空", false);
                    return;
                }
            }
            nextStep();
        }

    }


    public void nextStep() {
        FreeUI_EntityCallBack<BaseResponseBean<EquCallOutStateEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<EquCallOutStateEntity>>
                (new TypeToken<BaseResponseBean<EquCallOutStateEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EquCallOutStateEntity>> response) {
                showConfirmDialog(EquipmentTransferCompleteActivity.this, "提交成功");
                Intent intent = new Intent(EquipmentTransferCompleteActivity.this, EquipmentCallOutMaintainActivity.class);
                activity.startActivityForResult(intent,1);
                finish();
            }

            @Override
            public void onError(Response<BaseResponseBean<EquCallOutStateEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "UpdateEquipmentState");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","TRANSFER");//转单
        if(roleSelectLine.getText()!=null&&StringUtils.isNotBlank(roleSelectLine.getText().toString())){
            map.addProperty("ROLE",stateMap.get(roleSelectLine.getText().toString()).getKey());
        }
        map.addProperty("OBJECTRRN",bundle.getLong("OBJECTRRN"));
        map.addProperty("REMARK",remark.getText().toString().trim());
        NetUtils.requestPostNet("IF.BREEQUCALLOUTMAINTAIN", "Common", EquipmentTransferCompleteActivity.this, map, callBack);

    }

}
